import 'package:flutter/widgets.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:osc/utils/string_util.dart';

class WebWidget extends StatelessWidget {
  final String url;
  final String body;

  const WebWidget({Key key, this.url, this.body}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return InAppWebView(
      initialUrl: StringUtil.isEmpty(url)? "about:blank" : url,
      initialData: StringUtil.isEmpty(body)? null : InAppWebViewInitialData(data: StringUtil.createHtml(body)),
      initialHeaders: {

      },
      initialOptions: InAppWebViewWidgetOptions(
          inAppWebViewOptions: InAppWebViewOptions(

          )
      ),
      onWebViewCreated: (InAppWebViewController controller) {
      },
      onLoadStart: (InAppWebViewController controller, String url) {
      },
      onProgressChanged: (InAppWebViewController controller, int progress) {
      },
      onLoadError: (InAppWebViewController controller, String url, int code, String message) {
      },
    );
  }

  /*checkAuth(){
    int startIndex = 0;
    int endIndex = 0;
    String currentUrl = url;
    if(currentUrl.startsWith(Constant.OSC_OPEN_API_REDIRECT_URL_M + "?code=") && currentUrl.contains("&state=")){
      startIndex = currentUrl.indexOf("?code=") + 6;
      endIndex = currentUrl.indexOf("&state=");
      String code = currentUrl.substring(startIndex, endIndex);
      PdLog.printLog("****** currentUrl = "+currentUrl);
      PdLog.printLog("****** code = "+code);
      SpHelper.putObject("OSC_CODE", code);
      OscSp.instance.setCode(code);

      PopResult result = PopResult(id: PopResult.ID_OSC_OAUTH, success: true, object: code);
      if(mounted) {
        Navigator.pop<PopResult>(context, result);
      }
    }
    if (mounted) {
      setState(() {
        _history.add('onUrlChanged: $url');
      });
    }
  }*/
}
